/**
 * 
 */
package vn.lumi.smarthome.network;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Parcel;
import android.util.Log;


/**
 * @author hieu
 *
 */
public class ChangePassWord extends AppMsg{
	private static final int LENGTH = 24;
	private String newpass;
	public ChangePassWord(int Msg_id, int uid, int sid, String newpass){
		super(NetMessage.CMD_APP_CHANGE_PASSWORD, Msg_id, uid, sid);
		this.newpass = newpass;
		
	}
	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#generateByteArray()
	 */
	@Override
	public byte[] generateByteArray() {
		// TODO Auto-generated method stub
		byte[] result = new byte[LENGTH];
		result[0] = (byte) this.Cmd_id;
		result[1] = (byte) this.Msg_id;
		result[2] = (byte) this.user_id;
		result[3] = (byte) this.session_id;
		byte[] NewPass = null;
		Log.e("ban tin","changePassWord" + newpass);
		try {
			NewPass = newpass.getBytes("UTF-16BE");
			Log.e("ban tin","changePassWord" + Arrays.toString(NewPass));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i = 0; i<20; i++){
			if(i<NewPass.length) result[4+i] = NewPass[i];
			else result[4+i] = (byte) 0x00;
		}
		Log.e("ban tin","changePassWord" + Arrays.toString(result));
		return result;
	}
	/* (non-Javadoc)
	 * @see android.os.Parcelable#describeContents()
	 */
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
	 */
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		
	}

	

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#doIfSendSuccessful(android.content.Context)
	 */
	@Override
	public void doIfSendSuccessful(Context context) {
		// TODO Auto-generated method stub
		// thong bao cho UI
		Intent FilteredPwdResponse = new Intent("ackMessageChangePwd");
		context.sendBroadcast(FilteredPwdResponse);
		
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#doIfSendFail(android.content.Context)
	 */
	@Override
	public void doIfSendFail(Context context) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#readFromParcel(android.os.Parcel)
	 */
	@Override
	public void readFromParcel(Parcel in) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#isCorrectReply(byte[], int)
	 */
	@Override
	public boolean isCorrectReply(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result = false;
		HC_ACK ack = new HC_ACK();
		if(ack.isCorrectFormat(input, length)){
			ack.parseMessage(input, length);
			Log.e("ban tin", "ackChangepwd" + ack.getMsg_id());
			Log.e("ban tin", "MsgId_pwd" + this.Msg_id);
			if(ack.getMsg_id() == this.Msg_id) result = true;
		}
		return result;
	}
	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#isCorrectBusy(byte[], int)
	 */
	@Override
	public boolean isCorrectBusy(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result =false;
		HC_ACK_BUSY ack_busy = new HC_ACK_BUSY();
		if(ack_busy.isCorrectFormatBusy(input, length)){
			ack_busy.parseMessage(input, length);
			if(ack_busy.getMsg_id() == this.Msg_id) result = true;
		}
		return result; 
	}
	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#doIfHcBusying()
	 */
	@Override
	public void doIfHcBusying() {
		// TODO Auto-generated method stub
		
	}

}
